﻿Imports VN.INS.BL.BC
Imports System.Data
Imports Microsoft.Office.Interop
Imports System.IO

Partial Class Devida_ReporteSeguimiento_POA_Especifico
    Inherits System.Web.UI.Page

#Region "Constantes"
    Dim tmpChartName1 As String = "SegPOA1.jpg"
    Dim tmpChartName2 As String = "SegPOA2.jpg"
#End Region

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'capturamos parametros
        ViewState("Nombre") = Request.QueryString("Nombre").ToString
        ViewState("POA") = Request.QueryString("POA").ToString
        ViewState("PPER") = Request.QueryString("PPER").ToString
        ViewState("PPER_ID") = Request.QueryString("PPER_ID").ToString
        ViewState("Ejecutor") = Request.QueryString("Ejecutor").ToString
        ViewState("Eje_Des") = Request.QueryString("Eje_Des").ToString
        ViewState("Persona") = Request.QueryString("Persona").ToString
        'cargamos etiquetas
        lblPPER.Text = ViewState("PPER")
        lblNombre.Text = ViewState("Nombre")
        lblEjecutor.Text = ViewState("Eje_Des")
        lblCodigo.Text = ViewState("POA").ToString
        lblRegion.Text = Request.QueryString("REGION").ToString
        lblMonitoreador.Text = Request.QueryString("Monitoreador").ToString
        lblEspacialista.Text = Request.QueryString("Especialista").ToString
        Cargar()
    End Sub

    Sub Cargar()
        'carga de grilla observaciones
        Dim objReporte As ReporteBC
        Dim objDT As DataTable
        Try
            objReporte = New ReporteBC
            objDT = New DataTable
            objDT = objReporte.EvolucionPOA(ViewState("POA"))
            gvObservaciones.DataSource = objDT
            gvObservaciones.DataBind()
            Chart(objDT)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Public Sub Chart(ByVal objDt As DataTable)
        'Chart1
        Try
            Dim x() As String = New String(objDt.Rows.Count - 1) {}
            Dim y() As Double = New Double(objDt.Rows.Count - 1) {}
            For i As Int16 = 0 To objDt.Rows.Count - 1
                x(i) = objDt.Rows(objDt.Rows.Count - i - 1).Item("DESCRIPCION")
                y(i) = objDt.Rows(objDt.Rows.Count - i - 1).Item("DEVENGADO")
            Next

            Chart1.Series("Devengado").Points.DataBindXY(x, y)
            Chart1.ChartAreas("ChartArea1").AxisX.Minimum = 1
            Chart1.ChartAreas("ChartArea1").AxisX.Maximum = objDt.Rows.Count
            Chart1.ChartAreas("ChartArea1").AxisX.Interval = 1
            Dim colorSet() As Drawing.Color = New Drawing.Color(3) {}
            colorSet(0) = Drawing.Color.FromArgb(146, 208, 80)
            colorSet(1) = Drawing.Color.FromArgb(128, 0, 64)
            colorSet(2) = Drawing.Color.FromArgb(0, 153, 255)
            colorSet(3) = Drawing.Color.FromArgb(13, 73, 125)

            Chart1.Palette = DataVisualization.Charting.ChartColorPalette.None
            Chart1.PaletteCustomColors = colorSet
            Chart1.Titles(0).Text = "Avance Financiero Devengado"
        Catch ex As Exception
            Throw ex
        End Try
        'Chart2
        Try
            Dim x() As String = New String(objDt.Rows.Count - 1) {}
            Dim y() As Double = New Double(objDt.Rows.Count - 1) {}
            For i As Int16 = 0 To objDt.Rows.Count - 1
                x(i) = objDt.Rows(objDt.Rows.Count - 1 - i).Item("DESCRIPCION")
                y(i) = objDt.Rows(objDt.Rows.Count - 1 - i).Item("GIRADO")
            Next

            Chart2.Series("Girado").Points.DataBindXY(x, y)
            Chart2.ChartAreas("ChartArea1").AxisX.Minimum = 1
            Chart2.ChartAreas("ChartArea1").AxisX.Maximum = objDt.Rows.Count
            Chart2.ChartAreas("ChartArea1").AxisX.Interval = 1
            Dim colorSet() As Drawing.Color = New Drawing.Color(3) {}
            colorSet(0) = Drawing.Color.FromArgb(128, 0, 64)
            colorSet(1) = Drawing.Color.FromArgb(146, 208, 80)
            colorSet(2) = Drawing.Color.FromArgb(0, 153, 255)
            colorSet(3) = Drawing.Color.FromArgb(13, 73, 125)

            Chart2.Palette = DataVisualization.Charting.ChartColorPalette.None
            Chart2.PaletteCustomColors = colorSet
            Chart2.Titles(0).Text = "Avance Financiero Girado"
        Catch ex As Exception
            Throw ex
        End Try
        Dim imgPath1 As String = HttpContext.Current.Request.PhysicalApplicationPath + tmpChartName1
        Chart1.SaveImage(imgPath1)
        Dim imgPath2 As String = HttpContext.Current.Request.PhysicalApplicationPath + tmpChartName2
        Chart2.SaveImage(imgPath2)
    End Sub

    Public Sub ExportarExcel()
        Dim sb As StringBuilder = New StringBuilder()
        Dim sw As StringWriter = New StringWriter(sb)
        Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)
        Dim pagina As Page = New Page
        'CREAR TITULO
        Dim gvtitle1 As New Table
        Dim titulo1 As New TableCell()
        titulo1.RowSpan = 1
        titulo1.ColumnSpan = 5
        titulo1.Text = "SEGUIMIENTO PROYECTO / ACTIVIDAD"
        titulo1.Font.Name = "Calibri"
        titulo1.Font.Size = 13
        titulo1.Font.Bold = True
        titulo1.BackColor = Drawing.Color.White
        titulo1.HorizontalAlign = HorizontalAlign.Center
        titulo1.VerticalAlign = VerticalAlign.Middle
        Dim gvtitle2 As New Table
        Dim titulo2 As New TableCell()
        titulo2.RowSpan = 1
        titulo2.ColumnSpan = 10
        titulo2.Text = " "
        titulo2.Font.Name = "Calibri"
        titulo2.Font.Size = 11
        titulo2.Font.Bold = True
        titulo2.BackColor = Drawing.Color.White
        titulo2.HorizontalAlign = HorizontalAlign.Center
        titulo2.VerticalAlign = VerticalAlign.Middle
        Dim tb0 As New Table
        Dim tit0, tit02, celdaVacia0 As New TableCell()
        tit0.RowSpan = 1
        tit0.Text = "PPER: "
        tit0.Font.Name = "Calibri"
        tit0.Font.Size = 11
        tit0.Font.Bold = True
        tit0.BackColor = Drawing.Color.White
        tit0.HorizontalAlign = HorizontalAlign.Left
        tit02.Text = lblPPER.Text
        celdaVacia0.Text = " "
        Dim tb1 As New Table
        Dim tit1, tit12, tit13, celdaVacia1 As New TableCell()
        tit1.RowSpan = 1
        tit1.Text = "Nombre: "
        tit1.Font.Name = "Calibri"
        tit1.Font.Size = 11
        tit1.Font.Bold = True
        tit1.BackColor = Drawing.Color.White
        tit1.HorizontalAlign = HorizontalAlign.Left
        tit1.VerticalAlign = VerticalAlign.Top
        tit12.Text = lblNombre.Text
        tit13.Text = "Código: " + lblCodigo.Text
        tit13.Font.Name = "Calibri"
        tit13.Font.Size = 11
        tit13.Font.Bold = False
        tit13.BackColor = Drawing.Color.White
        tit13.HorizontalAlign = HorizontalAlign.Left
        tit13.VerticalAlign = VerticalAlign.Top
        celdaVacia1.Text = " "
        Dim tb2 As New Table
        Dim tit2, tit22, celdaVacia2 As New TableCell()
        tit2.RowSpan = 1
        tit2.Text = "Ejecutor: "
        tit2.Font.Name = "Calibri"
        tit2.Font.Size = 11
        tit2.Font.Bold = True
        tit2.BackColor = Drawing.Color.White
        tit2.HorizontalAlign = HorizontalAlign.Left
        tit22.Text = lblEjecutor.Text
        celdaVacia2.Text = " "
        Dim tb3 As New Table
        Dim tit3, tit32, celdaVacia3 As New TableCell()
        tit3.RowSpan = 1
        tit3.Text = "Region: "
        tit3.Font.Name = "Calibri"
        tit3.Font.Size = 11
        tit3.Font.Bold = True
        tit3.BackColor = Drawing.Color.White
        tit3.HorizontalAlign = HorizontalAlign.Left
        tit3.VerticalAlign = VerticalAlign.Middle
        tit32.Text = lblRegion.Text
        celdaVacia3.Text = " "
        Dim tb4 As New Table
        Dim tit4, tit42, celdaVacia4 As New TableCell()
        tit4.RowSpan = 1
        tit4.Text = "Monitoreador: "
        tit4.Font.Name = "Calibri"
        tit4.Font.Size = 11
        tit4.Font.Bold = True
        tit4.BackColor = Drawing.Color.White
        tit4.HorizontalAlign = HorizontalAlign.Left
        tit4.VerticalAlign = VerticalAlign.Middle
        tit42.Text = lblMonitoreador.Text
        celdaVacia4.Text = " "
        Dim tb5 As New Table
        Dim tit5, tit52, celdaVacia5 As New TableCell()
        tit5.RowSpan = 1
        tit5.Text = "Especialista: "
        tit5.Font.Name = "Calibri"
        tit5.Font.Size = 11
        tit5.Font.Bold = True
        tit5.BackColor = Drawing.Color.White
        tit5.HorizontalAlign = HorizontalAlign.Left
        tit5.VerticalAlign = VerticalAlign.Middle
        tit52.Text = lblEspacialista.Text
        celdaVacia5.Text = " "
        Dim headertitulo1 As New TableRow()
        headertitulo1.Cells.Add(titulo1)
        Dim headertitulo2 As New TableRow()
        headertitulo2.Cells.Add(titulo2)
        Dim h0 As New TableRow()
        h0.Cells.Add(tit0)
        h0.Cells.Add(celdaVacia0)
        h0.Cells.Add(tit02)
        Dim h1 As New TableRow()
        h1.Cells.Add(tit1)
        h1.Cells.Add(celdaVacia1)
        h1.Cells.Add(tit12)
        h1.Cells.Add(tit13)
        Dim h2 As New TableRow()
        h2.Cells.Add(tit2)
        h2.Cells.Add(celdaVacia2)
        h2.Cells.Add(tit22)
        Dim h3 As New TableRow()
        h3.Cells.Add(tit3)
        h3.Cells.Add(celdaVacia3)
        h3.Cells.Add(tit32)
        Dim h4 As New TableRow()
        h4.Cells.Add(tit4)
        h4.Cells.Add(celdaVacia4)
        h4.Cells.Add(tit42)
        Dim h5 As New TableRow()
        h5.Cells.Add(tit5)
        h5.Cells.Add(celdaVacia5)
        h5.Cells.Add(tit52)
        Dim imgPath1 As String = Request.Url.GetLeftPart(UriPartial.Authority) + VirtualPathUtility.ToAbsolute("~/" + tmpChartName1)
        Dim imgPath2 As String = Request.Url.GetLeftPart(UriPartial.Authority) + VirtualPathUtility.ToAbsolute("~/" + tmpChartName2)
        Dim ChartTable As String = "<Table ><tr><td></td><td></td><td></td><td></td><td></td></tr>" + _
                                    "<tr><td><img src='" + imgPath1 + "' \></td><td></td><td></td><td><img src='" + imgPath2 + "' \></td><td></td></tr>" + _
                                    "<tr><td></td><td></td><td></td><td></td><td></td></tr>" + _
                                    "<tr><td></td><td></td><td></td><td></td><td></td></tr>" + _
                                    "<tr><td></td><td></td><td></td><td></td><td></td></tr>" + _
                                    "<tr><td></td><td></td><td></td><td></td><td></td></tr>" + _
                                    "<tr><td></td><td></td><td></td><td></td><td></td></tr>" + _
                                    "<tr><td></td><td></td><td></td><td></td><td></td></tr>" + _
                                    "<tr><td></td><td></td><td></td><td></td><td></td></tr>" + _
                                    "<tr><td></td><td></td><td></td><td></td><td></td></tr>" + _
                                    "<tr><td></td><td></td><td></td><td></td><td></td></tr>" + _
                                    "<tr><td></td><td></td><td></td><td></td><td></td></tr>" + _
                                    "<tr><td></td><td></td><td></td><td></td><td></td></tr>" + _
                                    "<tr><td></td><td></td><td></td><td></td><td></td></tr>" + _
                                    "<tr><td></td><td></td><td></td><td></td><td></td></tr>" + _
                                    "<tr><td></td><td></td><td></td><td></td><td></td></tr>" + _
                                    "<tr><td></td><td></td><td></td><td></td><td></td></tr>" + _
                                    "</Table>"
        gvtitle1.Rows.Add(headertitulo1)
        gvtitle2.Rows.Add(headertitulo2)
        tb0.Rows.Add(h0)
        tb1.Rows.Add(h1)
        tb2.Rows.Add(h2)
        tb3.Rows.Add(h3)
        tb4.Rows.Add(h4)
        tb5.Rows.Add(h5)
        Dim form = New HtmlForm
        pagina.EnableEventValidation = False
        pagina.DesignerInitialize()
        pagina.Controls.Add(form)
        form.Controls.Add(gvtitle1)
        form.Controls.Add(gvtitle2)
        form.Controls.Add(tb0)
        form.Controls.Add(tb1)
        form.Controls.Add(tb2)
        form.Controls.Add(tb3)
        form.Controls.Add(tb4)
        form.Controls.Add(tb5)
        pagina.RenderControl(htw)

        Dim sb1 As StringBuilder = New StringBuilder()
        Dim sw1 As StringWriter = New StringWriter(sb1)
        Dim htw1 As HtmlTextWriter = New HtmlTextWriter(sw1)
        Dim pagina1 As Page = New Page
        Dim form1 = New HtmlForm
        pagina1.EnableEventValidation = False
        pagina1.DesignerInitialize()
        pagina1.Controls.Add(form1)
        form1.InnerHtml = ChartTable
        form1.Controls.Add(gvObservaciones)
        pagina1.RenderControl(htw1)

        Response.Clear()
        Response.Buffer = True
        Response.ContentType = "application/vnd.ms-excel"
        Response.AddHeader("Content-Disposition", "attachment;filename=ReporteEvolucion_" + Now.Date.Year.ToString + Now.Date.Month.ToString + Now.Date.Day.ToString + Now.Hour.ToString + Now.Minute.ToString + Now.Second.ToString + ".xls")
        Response.Charset = "UTF-8"
        Response.ContentEncoding = Encoding.Default
        Response.Write(sb.ToString())
        Response.Write(sb1.ToString())
        Response.End()
    End Sub

    Protected Sub btnExportar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExportar.Click
        ExportarExcel()
    End Sub

    Protected Sub Word()
        Dim gvtitle1 As New Table
        Dim titulo1 As New TableCell()
        titulo1.RowSpan = 1
        titulo1.ColumnSpan = 5
        titulo1.Text = "SEGUIMIENTO PROYECTO / ACTIVIDAD"
        titulo1.Font.Name = "Calibri"
        titulo1.Font.Size = 13
        titulo1.Font.Bold = True
        titulo1.BackColor = Drawing.Color.White
        titulo1.HorizontalAlign = HorizontalAlign.Center
        titulo1.VerticalAlign = VerticalAlign.Middle
        Dim headertitulo1 As New TableRow()
        headertitulo1.Cells.Add(titulo1)
        gvtitle1.Rows.Add(headertitulo1)
        Response.Clear()
        Response.Buffer = True
        Response.AddHeader("content-disposition",
        "attachment;filename=GridViewExport.doc")
        Response.Charset = ""
        Response.ContentType = "application/vnd.ms-word "
        Dim sb As StringBuilder = New StringBuilder()
        Dim sw As StringWriter = New StringWriter(sb)
        Dim htw As HtmlTextWriter = New HtmlTextWriter(sw)
        Dim pagina As Page = New Page
        Dim form = New HtmlForm
        gvObservaciones.AllowPaging = False
        gvObservaciones.DataBind()
        pagina.EnableEventValidation = False
        pagina.DesignerInitialize()
        pagina.Controls.Add(form)
        form.Controls.Add(gvtitle1)
        form.Controls.Add(gvObservaciones)
        pagina.RenderControl(htw)
        Response.Output.Write(sw.ToString())
        Response.Flush()
        Response.End()
    End Sub

    Protected Sub gvObservaciones_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvObservaciones.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Select Case e.Row.Cells(1).Text
                Case "1"
                    e.Row.Cells(1).BackColor = Drawing.Color.FromArgb(0, 128, 0)
                Case "2"
                    e.Row.Cells(1).BackColor = Drawing.Color.FromArgb(250, 150, 0)
                Case "3"
                    e.Row.Cells(1).BackColor = Drawing.Color.FromArgb(250, 50, 50)
            End Select
            e.Row.Cells(1).Text = ""
        End If
    End Sub

    Protected Sub btnCerrar_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnCerrar.Click
        Response.Redirect("Seguimiento_POA_Especifico.aspx")
    End Sub

End Class